package com.jinlejia.erp.base.unicorn;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import com.jinlejia.erp.BuildConfig;
import com.jinlejia.erp.R;
import com.jinlejia.erp.base.ApiConfig;
import com.jinlejia.erp.ui.common.MainActivity;
import com.jinlejia.erp.ui.common.WebViewActivity;
import com.jinlejia.erp.utils.SharePTool;
import com.jinlejia.feedback.idea.ui.FbIdeaMainActivity;
import com.qiyukf.unicorn.api.ConsultSource;
import com.qiyukf.unicorn.api.OnMessageItemClickListener;
import com.qiyukf.unicorn.api.RequestCallback;
import com.qiyukf.unicorn.api.StatusBarNotificationConfig;
import com.qiyukf.unicorn.api.UICustomization;
import com.qiyukf.unicorn.api.Unicorn;
import com.qiyukf.unicorn.api.YSFOptions;
import com.qiyukf.unicorn.api.YSFUserInfo;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public final class UnicornUtils {

    public static YSFOptions options(){
        return innerOptions(null);
    }
    public static YSFOptions innerOptions(SharePTool.UserInfoBean bean) {
        YSFOptions options = new YSFOptions();
        // 响应 url 点击事件
        OnMessageItemClickListener messageItemClickListener = (context, url) -> {
            if ("https://inhome/feedback.com".equals(url)) {
                Intent intent = new Intent();
                intent.setClass(context, FbIdeaMainActivity.class);
                context.startActivity(intent);
            } else {
                Intent intent = new Intent();
                intent.setClass(context, WebViewActivity.class);
                context.startActivity(intent);
            }
        };
        UICustomization uiCustomization = new UICustomization();
        if (bean!=null){
            uiCustomization.rightAvatar = ApiConfig.HEADER_URL()+bean.getImageUrl();
        }
        options.uiCustomization = uiCustomization;
        options.onMessageItemClickListener = messageItemClickListener;
        StatusBarNotificationConfig notificationConfig = new StatusBarNotificationConfig();
        notificationConfig.notificationSmallIconId = R.mipmap.jpush_notification_icon;
        notificationConfig.notificationEntrance = MainActivity.class;
        notificationConfig.bigIconUri = "http://";
        options.statusBarNotificationConfig = notificationConfig;
        return options;
    }

    public static void registerUnicorn(SharePTool.UserInfoBean bean){
        innerRegisterUnicorn(bean,null);
    }
    private static void innerRegisterUnicorn(SharePTool.UserInfoBean bean,RequestCallback<Void> callback) {
        String pin = bean.getPin();
        String imageUrl = String.valueOf(bean.getImageUrl());
        String userName =bean.getUserName();
        String telNum = bean.getTelNum();
        String titleLevel =bean.getTitleLevel();
        String email = bean.getEmail();
        String deptName = bean.getDeptName();
        String shareStoreName = bean.getShareStoreName();
        YSFUserInfo userInfo = new YSFUserInfo();
        userInfo.userId = pin;
        Unicorn.updateOptions(innerOptions(bean));
        JSONArray array = new JSONArray();
        try {
            for (int i = 0; i < 7; i++) {
                JSONObject object = new JSONObject();
                switch (i) {
                    case 0:
                        object.put("key", "real_name");
                        object.put("value", userName); //真实姓名
                        break;
                    case 1:
                        object.put("key", "mobile_phone");
                        object.put("value", telNum); //手机号
                        break;
                    case 2:
                        object.put("key", "email");
                        object.put("email", email); //电子邮箱
                        break;
                    case 3:
                        object.put("key", "avatar");
                        if (!TextUtils.isEmpty(imageUrl)) {
                            object.put("value", ApiConfig.HEADER_URL() + imageUrl); //头像
                        } else {
                            object.put("value", "");
                        }
                        break;
                    case 4:
                        object.put("index", 0);
                        object.put("key", "dept_name");
                        object.put("label", "部门");
                        if (!TextUtils.isEmpty(deptName)) {
                            object.put("value", deptName);
                        }
                        break;
                    case 5:
                        object.put("index", 1);
                        object.put("key", "dept_name");
                        object.put("label", "职务");
                        if (!TextUtils.isEmpty(titleLevel)) {
                            object.put("value", titleLevel);
                        }
                        break;
                    case 6:
                        object.put("index", 2);
                        object.put("key", "shareStoreName");
                        object.put("label", "共享店");
                        if (!TextUtils.isEmpty(shareStoreName)) {
                            object.put("value", shareStoreName);
                        }
                        break;

                }
                array.put(object);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        userInfo.data = array.toString();
        userInfo.authToken = "http://www.baidu.com";
        if (callback==null){
            Unicorn.setUserInfo(userInfo);
        }else {
            Unicorn.setUserInfo(userInfo,callback);
        }
    }

    public static void registerAndOpenUnicorn(final Context context,SharePTool.UserInfoBean bean){
        Unicorn.updateOptions(innerOptions(bean));
        innerRegisterUnicorn(bean,new RequestCallback<Void>(){

            @Override
            public void onSuccess(Void param) {
                openUnicorn(context);
            }

            @Override
            public void onFailed(int code) {

            }

            @Override
            public void onException(Throwable exception) {

            }
        });
    }

    public static void openUnicorn(Context context){
        String sourceTitle = "从推送进入的标题"+ BuildConfig.VERSION_NAME;
        String sourceUrl = "the Url was from home";
        String title = "客服";
        /**
         * 设置访客来源，标识访客是从哪个页面发起咨询的，用于客服了解用户是从什么页面进入。
         * 三个参数分别为：来源页面的url，来源页面标题，来源页面额外信息（保留字段，暂时无用）。
         * 设置来源后，在客服会话界面的"用户资料"栏的页面项，可以看到这里设置的值。
         */
        ConsultSource source = new ConsultSource(sourceUrl, sourceTitle, "这个没用");
        /**
         * 请注意： 调用该接口前，应先检查Unicorn.isServiceAvailable()，
         * 如果返回为false，该接口不会有任何动作
         *
         * @param context 上下文
         * @param title   聊天窗口的标题
         * @param source  咨询的发起来源，包括发起咨询的url，title，描述信息等
         */
        Unicorn.openServiceActivity(context, title, source);
    }
}
